﻿@model IEnumerable<Product>
@using bpms.Enums
@{
    ViewData["Title"] = "饮品管理";
}
@{
    var userTypeClaim = User.FindFirst("UserType")?.Value;
    var userType = !string.IsNullOrEmpty(userTypeClaim)
        ? (UserType)int.Parse(userTypeClaim)
        : UserType.Admin;
}
<h2>@ViewData["Title"]</h2>

<!-- 搜索表单 -->
<form asp-action="Index" method="get" class="mb-4">
    <div class="row g-2">
        <div class="col-6 col-md-4 col-lg-3">
            <div class="input-group">
                <input type="text"
                       name="searchKey"
                       class="form-control form-control-sm"
                       placeholder="编号/名称"
                       value="@ViewBag.SearchKey">
                <button type="submit" class="btn btn-primary btn-sm">
                    <i class="bi bi-search"></i>搜索
                </button>
            </div>
        </div>
        @if (userType == (int)UserType.Admin)
        {
            <div class="col-auto">
                <a asp-action="Add" class="btn btn-success btn-sm">
                    <i class="bi bi-plus-circle"></i> 新增
                </a>
            </div>
        }
    </div>
</form>

<table class="table table-hover">
    <thead>
        <tr>
            <th>@Html.DisplayNameFor(m => m.ProductNo)</th>
            <th>@Html.DisplayNameFor(m => m.Name)</th>
            <th>@Html.DisplayNameFor(m => m.Price)</th>
            <th>@Html.DisplayNameFor(m => m.Stock)</th>
            <th>@Html.DisplayNameFor(m => m.LimitStock)</th>
            <th>@Html.DisplayNameFor(m => m.ProduceDate)</th>
            <th>@Html.DisplayNameFor(m => m.QualityPeriod)</th>
            <th>@Html.DisplayNameFor(m => m.Supplier)</th>
            <th>图片</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.ProductNo</td>
                <td>@item.Name</td>
                <td>@item.Price.ToString("C")</td>
                <td>@item.Stock</td>
                <td>@item.LimitStock</td>
                <td>@item.ProduceDate</td>
                <td>@item.QualityPeriod</td>
                <td>@item.Supplier</td>
                <td>
                    @if (!string.IsNullOrEmpty(item.Image))
                    {
                        <img src="@item.Image" class="img-thumbnail" style="max-width: 80px;" />
                    }
                </td>
                <td>
                    @if (item.Status == 0)
                    {
                        <span class="badge bg-secondary">
                            <i class="bi bi-x-circle me-1"></i>
                            未上架
                        </span>
                    }
                    else
                    {
                        <span class="badge bg-success">
                            <i class="bi bi-check-circle me-1"></i>
                            已上架
                        </span>
                    }
                </td>
                <td>
                    @if (userType == UserType.Admin)
                    {
                        <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-sm btn-primary">编辑</a>
                    }
                    <a asp-action="Detail" asp-route-id="@item.Id" class="btn btn-sm btn-info">详情</a>
                    @if (userType == UserType.Customer)
                    {
                    <a asp-action="Buy" asp-route-id="@item.Id" class="btn btn-sm btn-success">购买</a>
                    }
                    @if (userType == UserType.Admin)
                    {
                        <form asp-action="Delete" method="post" class="d-inline">
                            <input type="hidden" name="id" value="@item.Id" />
                            <button type="submit" class="btn btn-sm btn-danger"
                                    onclick="return confirm('确定删除？')">
                                <i class="bi bi-trash"></i>
                                删除
                            </button>
                        </form>
                        <a asp-action="UpOrDown" asp-route-id="@item.Id" asp-route-status="@item.Status"
                           class="btn btn-sm @(item.Status == 0 ? "btn-success" : "btn-danger")">
                            @(item.Status == 0 ? "上架" : "下架")
                        </a>

                    }
                </td>
            </tr>
        }
    </tbody>
</table>